﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

using prjKQXS_DLL;

namespace prjKQXS_UI.Security
{
    public partial class frmPhanQuyenMay : DevExpress.XtraEditors.XtraForm
    {
        public frmPhanQuyenMay()
        {
            InitializeComponent();
        }

        private void frmPhanQuyenMay_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("Select * from May_Chu Where isPublic=1", conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                gcMay.DataSource = dt;
            }
            catch
            {
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand("Insert into May_Chu(Ten_May) values(@tenmay)", conn);
            cmd.Parameters.AddWithValue("@tenmay", "Nhập tên máy mới");
            cmd.ExecuteNonQuery();
            loadData();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            int id = 0;
            id = int.Parse(gvMay.GetFocusedDataRow()["ID"].ToString());
            SqlCommand cmd = new SqlCommand("Delete from May_Chu Where ID=@ID", conn);
            cmd.Parameters.AddWithValue("@ID", id);
            cmd.ExecuteNonQuery();
            loadData();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gvMay_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            clsCommon.updateRowValue("May_Chu", e.Column.FieldName, "Where ID=" + gvMay.GetFocusedDataRow()["ID"].ToString(), e.Value);
        }

        private void gvMay_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            //MessageBox.Show(e.Column.ToString());
            if (gvMay.FocusedColumn.FieldName == "isMayChu")
            {
                if (e.Value.ToString().ToUpper() == "TRUE" || e.Value.ToString() == "1")
                {
                    clsCommon.updateRowValue("May_Chu", e.Column.FieldName, "Where ID=" + gvMay.GetFocusedDataRow()["ID"].ToString(), 1);
                    SqlConnection conn = new SqlConnection(BienTc.connString);
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    int id = 0;
                    id = int.Parse(gvMay.GetFocusedDataRow()["ID"].ToString());
                    SqlCommand cmd = new SqlCommand("Update May_Chu set isMayChu=0 Where ID <>@ID", conn);
                    cmd.Parameters.AddWithValue("@ID", id);
                    cmd.ExecuteNonQuery();
                    loadData();
                }
                else
                {
                    clsCommon.updateRowValue("May_Chu", e.Column.FieldName, "Where ID=" + gvMay.GetFocusedDataRow()["ID"].ToString(), e.Value);
                }
            }
        }
    }
}